#include"../Driver_PIC/system.h"
/******************************************************************************/


/***************************定义用于产生时基的变量*********************************/
#define TIMER0_CYCLE               5                /*timer0 中断周期，ms*/
#define COUNTER_50MS              (50/TIMER0_CYCLE)
#define COUNTER_100MS             (100/TIMER0_CYCLE)
#define COUNTER_1S                (1000/TIMER0_CYCLE)
#define COUNTER_2S                (2000/TIMER0_CYCLE)
#define COUNTER_3S                (3000/TIMER0_CYCLE)
#define COUNTER_10S               (10000/TIMER0_CYCLE)
#define COUNTER_60S               (60000/TIMER0_CYCLE)

unsigned int Counter_50ms  = 0;
unsigned int Counter_100ms = 0;
unsigned int Counter_1s    = 0;
unsigned int Counter_2s    = 0;
unsigned int Counter_3s    = 0;
unsigned int Counter_10s   = 0;
unsigned int Counter_60s   = 0;

bit bPulse_10ms  = 0;
bit bPulse_50ms  = 0;
bit bPulse_100ms = 0;
bit bPulse_1s    = 0;
bit bPulse_2s    = 0;
bit bPulse_3s    = 0;
bit bPulse_10s   = 0;
bit bPulse_60s   = 0;
                                           
unsigned char Timer0Over  = 0;                  /*定时器0溢出*/
bit bPulseTimer0 = 0;                  /*定时器溢出后产生一个扫描周期的脉冲*/

/******************************************************************************/


/*定时器0初始化*/
/*中断标志软件清0*/
extern unsigned int Timer0StartVal;

typedef union
{
    unsigned long full ;
    struct
    {
        unsigned int low ;
        unsigned int high ;
    };
} longtoint;

/*****************************************************************************/
/********************************TIMER0初始化**********************************/

unsigned int  Timer0StartVal;                               //定时器0预加载值

bit Timer0Init(char priority)
{
    volatile longtoint ValCal;
    if(priority)
        INTCON2bits.TMR0IP = 1;                             //优先级配置
    else
        INTCON2bits.TMR0IP = 0;
    INTCONbits.TMR0IE = 1;                                  //使能中断
    T0CONbits.T08BIT = 0 ;                                  //16位计数器
    T0CONbits.T0CS = 0 ;                                    //对SystemClock计数
    T0CONbits.T0SE = 0 ;                                    //外部时钟时选择上升沿计数
    T0CONbits.PSA =0   ;                                    //使用预分频器
    T0CONbits.T0PS =0;                                      //8分频
    ValCal.full = (SystemClock/8/1000) * TIMER0_CYCLE - 8;      //初始值计算
    if(ValCal.full > 0xFFFF)
        return 0;
    Timer0StartVal = 0xFFFF-ValCal.full;
    return 1;
}
/********************************TIMER0初始化结束*******************************/
/*****************************************************************************/

/*****************************************************************************/
/*********************************TIMER0运行**********************************/
/*
 *用于产生系统时基
 */

__inline void Timer0Run()
{
    Counter_50ms  += Timer0Over;
    Counter_100ms += Timer0Over;
    Counter_1s    += Timer0Over;
    Counter_2s    += Timer0Over;
    Counter_3s    += Timer0Over;
    Counter_10s   += Timer0Over;
    Counter_60s   += Timer0Over;

    bPulseTimer0   = 1;
    
    if(Counter_50ms >= COUNTER_50MS)
    {
        Counter_50ms -= COUNTER_50MS;
        bPulse_50ms = 1;
    }
    if(Counter_100ms >= COUNTER_100MS)
    {
        Counter_100ms -= COUNTER_100MS;
        bPulse_100ms = 1;
    }
    if(Counter_1s >= COUNTER_1S)
    {
        Counter_1s -= COUNTER_1S;
        bPulse_1s = 1;
    }
    if(Counter_2s >= COUNTER_2S)
    {
        Counter_2s -= COUNTER_2S;
        bPulse_2s = 1;
    }
    if(Counter_3s >= COUNTER_3S)
    {
        Counter_3s -= COUNTER_3S;
        bPulse_3s = 1;
    }
    if(Counter_10s >= COUNTER_10S)
    {
        Counter_10s -= COUNTER_10S;
        bPulse_10s = 1;
    }
    if(Counter_60s >= COUNTER_60S)
    {
        Counter_60s -= COUNTER_60S;
        bPulse_60s = 1;
    }
    Timer0Over = 0;
}

__inline void ClearPulse()
{
    bPulseTimer0 = 0;
    bPulse_50ms  = 0;
    bPulse_100ms = 0;
    bPulse_1s    = 0;
    bPulse_2s    = 0;
    bPulse_3s    = 0;
    bPulse_10s   = 0;
    bPulse_60s   = 0;
}
/************************************TIMER0运行*******************************/
/*****************************************************************************/
